<!--
 * @author  dumeng
答题统计
-->
<template>
  <section class="special-statistics">
    <section class="main-search">
      <el-row :gutter="10">
        <el-col :span="12">
          <section class="search-item">
            <div class="item-label">部门</div>
            <el-row :gutter="10">
              <el-col :span="12">
                <el-cascader ref="org" v-model="search.orgId" style="width:100%" collapse-tags clearable :props="orgProps" />
              </el-col>
              <el-col :span="6" style="line-height: 35px;">
                <el-checkbox v-model="search.all">部门所有人员</el-checkbox>
              </el-col>
              <el-col :span="6" style="line-height: 35px;">
                <el-checkbox v-model="search.cndDeptSubFlag">包含下级部门</el-checkbox>
              </el-col>
            </el-row>
          </section>
        </el-col>
        <template v-if="!search.all">
          <el-col :span="6">
            <section class="search-item">
              <div class="item-label">姓名</div>
              <el-input v-model="search.cndUserRealName" placeholder="请输入姓名" />
            </section>
          </el-col>
          <el-col :span="6">
            <section class="search-item">
              <div class="item-label">手机号</div>
              <el-input v-model="search.cndUserphoneNumber" placeholder="请输入手机号" />
            </section>
          </el-col>
        </template>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="8">
          <section class="search-item">
            <div class="item-label">所属答题</div>
            <el-select v-model="search.breakthroughDailyId" multiple collapse-tags filterable placeholder="请选择答题">
              <el-option v-for="item in rushs" :key="item.id" :label="item.activityName" :value="item.id" />
            </el-select>
          </section>
        </el-col>
        <el-col :span="8">
          <section class="search-item">
            <div class="item-label">开始时间</div>
            <el-date-picker v-model="search.startTime" type="datetime" placeholder="选择日期" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
          </section>
        </el-col>
        <el-col :span="8">
          <section class="search-item">
            <div class="item-label">结束时间</div>
            <el-date-picker v-model="search.endTime" type="datetime" placeholder="选择日期" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
          </section>
        </el-col>
      </el-row>
      <section class="search-item" style="text-align: center;">
        <el-button type="primary" @click="handleStart">开启统计任务</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
      </section>
    </section>
    <section class="main-table">
      <section class="table-title">
        <section class="title-txt">答题统计</section>
        <section />
      </section>
      <v-table :list="list" tag="specialstatistics" :list-loading="listLoading" :filter-column="filtercolumn" :selection="false">
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            <el-button type="text" size="mini" @click="handleExport(scope.row)">导出</el-button>
            <el-button type="text" size="mini" @click="handleAgain(scope.row)">重新统计</el-button>
            <el-button type="text" size="mini" @click="handleView(scope.row)">查看明细</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
    <el-drawer title="统计详情" :visible.sync="drawer" direction="ttb" size="100%" destroy-on-close>
      <v-view :id="id" :obj="obj" />
    </el-drawer>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import VView from './../components/view'
import { getComAndOrgTree, queryRush, insert, again, queryPage, del, exportMask, exportNoMask } from './../api/noStatistics'
import { exportSign } from '@/utils/fileImportOrExportSign'
import { exportCheck } from '@/utils/sensitiveInfoExportCheck'
import CryptoJS from '@/utils/aesJiaJM'
export default {
  name: 'noStatistics',
  components: { Pagination, VTable, VView },
  data() {
    return {
      search: {
        cndUserRealName: '',
        cndUserphoneNumber: '',
        orgId: [],
        all: false,
        cndDeptSubFlag: false,
        startTime: '',
        endTime: '',
        breakthroughDailyId: []
      },
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      listLoading: false,
      list: [],
      rushs: [],
      drawer: false,
      id: '',
      orgProps: {
        label: 'name',
        value: 'id',
        multiple: true,
        checkStrictly: true,
        lazy: true,
        lazyLoad(node, resolve) {
          let params = {}
          if (node.level !== 0) {
            if (node.data.deptId !== '') {
              params = {
                parentId: node.data.orgId,
                deptPId: node.data.deptId
              }
            } else {
              params = {
                parentId: node.data.id
              }
            }
          }
          getComAndOrgTree(params).then(res => {
            resolve(res.data.data)
          }).catch(() => {
            resolve([])
          })
        }
      },
      obj: {}
    }
  },
  created() {
    this.getList()
    this.getRush()
  },
  methods: {
    filtercolumn(scope, item) {
      if (item === 'taskRunningState') {
        return scope.row[item] === '0' ? '待执行' : scope.row[item] === '1' ? '执行中' : '已结束'
      } else {
        return scope.row[item]
      }
    },

    handleStart() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        taskType: 'ACTIVITY',
        cndUserRealName: this.search.cndUserRealName,
        cndUserphoneNumber: this.search.cndUserphoneNumber,
        cndDeptIds: this.search.orgId.length > 0 ? this.search.orgId.join(',') : '',
        cndDeptSubFlag: this.search.cndDeptSubFlag ? 1 : 0,
        cndDeptUserSubFlag: this.search.all ? 1 : 0,
        cndStartTime: this.search.startTime,
        cndEndTime: this.search.endTime,
        cndBreakthroughIds: this.search.breakthroughDailyId.length > 0 ? this.search.breakthroughDailyId.join(',') : ''
      }
      return new Promise((resolve, reject) => {
        insert(params).then(() => {
          loading.close()
          this.$message({
            title: '成功',
            message: '统计任务开启成功',
            type: 'success',
            duration: 2000
          })
          this.handleReset()
          this.listQuery.page = 1
          this.getList()
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    },

    handleReset() {
      this.search.cndUserRealName = ''
      this.search.cndUserphoneNumber = ''
      this.search.cndDeptSubFlag = false
      this.search.orgId = []
      this.search.all = false
      this.search.startTime = ''
      this.search.endTime = ''
      this.search.breakthroughDailyId = []
    },

    handleExport(row) {
      const obj = exportSign({ taskId: row.id }).obj
      exportCheck('BREAK_THROUGH', row.id, exportMask, exportNoMask, obj, '答题统计.xlsx')
    },

    handleView(row) {
      const obj = {
        org: row.cndDeptIdsValue ? row.cndDeptIdsValue.split(',') : '',
        name: row.cndUserRealName,
        phone: row.cndUserphoneNumber,
        all: row.all,
        down: row.cndDeptSubFlag,
        startTime: row.cndStartTime,
        endTime: row.cndEndTime,
        rush: row.cndBreakthroughIdsValue ? row.cndBreakthroughIdsValue.split(',') : ''
      }
      this.obj = Object.assign({}, obj)
      this.id = row.id
      this.drawer = true
    },

    handleAgain(row) {
      this.$confirm('确定要重新统计吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          again(row.id).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '重新统计成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => { })
    },

    handleDelete(row) {
      this.$confirm('确定删除该统计任务吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          del(row.id).then(() => {
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            reject(error)
          })
        })
      }).catch(() => { })
    },

    getRush() {
      return new Promise((resolve, reject) => {
        queryRush({ status: '' }).then(res => {
          this.rushs = res.data.data || []
        }).catch(error => {
          reject(error)
        })
      })
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        taskType: 'ACTIVITY',
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.special-statistics {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 10px;
    .search-item {
      margin-bottom: 10px;
      &.button {
        text-align: center;
      }
      .item-label {
        margin-bottom: 5px;
        font-size: 14px;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
      }

      .el-input,
      .el-select {
        width: 100%;
      }
    }
  }

  .main-table {
    background-color: #fff;
    .table-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      .title-txt {
        font-size: 16px;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
      }
    }
  }

  .el-drawer__wrapper {
    .el-drawer__header {
      margin-bottom: 10px;
    }
    .el-drawer__header span:focus {
      outline: none;
    }
    .el-drawer__close-btn:focus {
      outline: none;
    }
  }
}
</style>
